﻿using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using XFControls.Utils;
using XFSenseUI;
using XFControls.XFPanels;
using XFSenseUI.XFItems;
using System.IO;
using XFSenseUI.XFItems.SenseControls;
using XFControls.XFPanels.XFPanelItems;
using XFControls.XFPanels.XFPanelModals;

namespace XDAFacebook
{
    public partial class SettingsMenu : Form
    {
        private string _domain = Settings.Domain;
        private string _picExt = Settings.pictureFileExtensions;
        private string _lang = Settings.language;
        private string _skin = Settings.Skin;
        private bool _resize = Settings.ResizeImages;
        private bool _useEmptyProxy = Settings.useEmptyProxy;
        private int _position = Settings.position;
        private int _imgPerLine = Settings.PhotosPerLine;
        private int _concurDownload = Settings.ConcurrentDownload;

        public SettingsMenu()
        {
            InitializeComponent();

            var list = new SenseListPanel();
            list.Attributes |= PanelAttributes.ScrollPast;
            list.ScrollbarColor = Color.Gray;

            xfPanelContainer.SetHeader(senseHeader);
            xfPanelContainer.SetMainPanel(list);

            senseHeader.CenterText = Settings.LocalizationStrings.settings;

            var divCat = new SenseItemDivider
            {
                Text = Settings.LocalizationStrings.appSettingsHeader
            };

            SenseItemMoreItems imageSettings = imageSettingsSetup();

            var divApp = new SenseItemDivider
            {
                Text = Settings.LocalizationStrings.appSettingsHeader
            };

            var position = new SenseItemDropDown
            {
                Description = Settings.LocalizationStrings.position
            };
            position.Options.Add(Settings.LocalizationStrings.position_gps);
            position.Options.Add(Settings.LocalizationStrings.position_both);
            position.Options.Add(Settings.LocalizationStrings.position_cellID);

            switch (Settings.position)
            {
                case 0:
                    position.Selected = Settings.LocalizationStrings.position_gps;
                    break;
                case 1:
                    position.Selected = Settings.LocalizationStrings.position_both;
                    break;
                case 2:
                    position.Selected = Settings.LocalizationStrings.position_cellID;
                    break;
            }

            position.SelectionChanged += (o, e) =>
            {
                if (e.Text == Settings.LocalizationStrings.position_gps)
                {
                    _position = 0;
                }
                else if (e.Text == Settings.LocalizationStrings.position_both)
                {
                    _position = 1;
                }
                else
                {
                    _position = 2;
                }
 
            };


            var proxy = new SenseItemDropDown
            {
                Description = Settings.LocalizationStrings.proxy_settings
            };
            proxy.Options.Add(Settings.LocalizationStrings.proxy_default);
            proxy.Options.Add(Settings.LocalizationStrings.proxy_empty);

            if (Settings.useEmptyProxy)
            {
                proxy.Selected = Settings.LocalizationStrings.proxy_empty;
            }
            else
            {
                proxy.Selected = Settings.LocalizationStrings.proxy_default;
            }
            proxy.SelectionChanged += (o, e) =>
            {
                if (e.Text == Settings.LocalizationStrings.proxy_empty)
                {
                    _useEmptyProxy = true;
                }
                else
                {
                    _useEmptyProxy = false;
                }
            };

            var domain = new SenseItemTextBox
            {
                Description = Settings.LocalizationStrings.domainDescription,
                Text = _domain
            };
            domain.TextChanged += (o, e) =>
                {
                    _domain = e.Text;
                };

            var lang = new SenseItemDropDown
                                         {
                                             Description = Settings.LocalizationStrings.langDescription,
                                             Selected = _lang
                                         };
            lang.SelectionChanged += (o, e) =>
                {
                    _lang = e.Text;
                };

            lang.Options.Add("English");

            if (!Directory.Exists(Settings.localizationDir))
                Directory.CreateDirectory(Settings.localizationDir);

            foreach (var langFile in Directory.GetFiles(Settings.localizationDir))
            {
                var l = langFile.Split('\\').Last();
                l = l.Split('.').First();
                if (l == Settings.blankLocFile)
                    continue;

                lang.Options.Add(l);
            }

            var genLang = new SenseItem { MainText = Settings.LocalizationStrings.genBlankLangFile };
            genLang.Click += (o, e) =>
            {
                Settings.SaveBlankLang();
                MessageBox.Show(Settings.LocalizationStrings.createdSuccessfully + "  " + Settings.localizationDir + "\\" + Settings.blankLocFile + ".txt");
            };

            list.Add(divCat);
            list.Add(imageSettings);
            list.Add(divApp);
            list.Add(domain);
            list.Add(position);
            list.Add(proxy);
            list.Add(lang);
            list.Add(genLang);
        }

        private SenseItemMoreItems imageSettingsSetup()
        {
            var ret = new SenseItemMoreItems
            {
                MainText = "Image Settings"
            };

            var rp = new SenseListPanel
            {
                PanelName = "Image Settings"
            };

            var skin = new SenseItemDropDown
                       {
                           Description = Settings.LocalizationStrings.AppSkin
                       };
            skin.Options.Add(Settings.LocalizationStrings.Default);

            try
            {
                foreach (var skinDir in Directory.GetDirectories(Settings.SkinDir))
                {
                    var l = skinDir.Split('\\').Last();
                    skin.Options.Add(l);
                }
            }
            catch (DirectoryNotFoundException) { }

            skin.Selected = Settings.Skin;

            skin.SelectionChanged += (o, e) =>
            {
                _skin = e.Text;
            };

            var picExt = new SenseItemTextBox
            {
                Description = Settings.LocalizationStrings.picExtDescription,
                Text = _picExt
            };
            picExt.TextChanged += (o, e) =>
            {
                _picExt = e.Text;
            };

            var resizeImages = new SenseItemControl
            {
                Control = new SenseControlCheckbox
                {
                    Checked = _resize
                },
                MainText = Settings.LocalizationStrings.resizeImages
            };

            resizeImages.Control.Click += (o, e) =>
            {
                _resize = ((SenseControlCheckbox)resizeImages.Control).Checked;
            };

            var imgPerLine = new SenseItemNumber
            {
                MultiLine = true,
                MainText = "Number of images per line",
                SecondaryText = "Setting for Albums. Default 3",
                Value = _imgPerLine
            };

            imgPerLine.Change += (o, e) =>
                {
                    _imgPerLine = imgPerLine.Value;
                };

            var concurDownload = new SenseItemNumber
            {
                MultiLine = true,
                MainText = "Number of Concurrent Image Downloads",
                SecondaryText = "The lower the number the better the performance. Default 6",
                Value = _concurDownload
            };

            concurDownload.Change += (o, e) =>
                {
                    _concurDownload = concurDownload.Value;
                };


            var clearCache = new SenseItem
            {
                MainText = "Clear Image Cache",
                ClickResults = XFItemClickResult.CustomModal
            };

            clearCache.Click += (o, e) =>
            {
                var modal = rp.ShowModal(XFModal.Bottom, "Are you sure you want to clear the image cache?");

                modal.OnSuccessAction += (o2, e2) => XFGraphics.ClearImageCache();
                modal.State = XFModalState.Expanding;

            };

            rp.AddSenseItem(skin);
            rp.AddSenseItem(picExt);
            rp.AddSenseItem(resizeImages);
            rp.AddSenseItem(imgPerLine);
            rp.AddSenseItem(concurDownload);
            rp.AddSenseItem(clearCache);

            ret.RightPanel = rp;

            return ret;
        }

        private void miSave_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Settings.LocalizationStrings.restartApp);

            Settings.Skin = _skin;
            Settings.Domain = _domain;
            Settings.pictureFileExtensions = _picExt;
            Settings.language = _lang;
            Settings.ResizeImages = _resize;
            Settings.ConcurrentDownload = _concurDownload;
            Settings.PhotosPerLine = _imgPerLine;
            Settings.position = _position;
            Settings.useEmptyProxy = _useEmptyProxy;

            Settings.SaveSettings();
            Close();
        }

        private void miCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}